package com.gf.user.controller;

import javax.naming.OperationNotSupportedException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.base.type.Role;
import com.gf.common.bean.DataRequest;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.exception.NotOperationEexception;
import com.gf.common.staticer.MessageRemainsHelper;
import com.gf.common.util.WebHelper;
import com.gf.fund.service.FundService;
import com.gf.message.service.MessageService;
import com.gf.user.model.User;
import com.gf.user.service.SuperService;
import com.gf.user.service.UserBaseService;

/**
 * 管理页面
 * 
 * @author zhuguifeng guifeng.net@gmail.com
 * @version 创建时间：2012-12-24 上午10:53:38
 */
@Controller
@RequestMapping("/manage")
public class ManageController extends BasicController {
	@Autowired
	private MessageService messageService;
	@Autowired
	private UserBaseService userBaseService;
	@Autowired
	private SuperService superService;
	@Autowired
	private FundService fundService;

	/**
	 * 管理主页
	 * 
	 * @return
	 * @throws OperationNotSupportedException
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView indexRequest() {
		User user = WebHelper.getSessionUser();
		String role = user.getRole().toString();
		if (!role.equals(Role.ADMIN.toString())
				&& !role.equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}
		boolean tag = false;
		if (role.equals(Role.SUPER.toString())) {
			tag = true;
		}
		int unReadMsgCount = MessageRemainsHelper.getRemains(user.getId());
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.addObject("unReadMsgCount", unReadMsgCount);
		mvc.addObject("tag", tag);
		mvc.setViewName(ViewPaths.MANAGE_INDEX);
		return mvc;
	}

	/**
	 * 充值记录页面<br>
	 * 超级管理员
	 * 
	 * @return
	 */
	@RequestMapping(value = "/fund", method = RequestMethod.GET)
	public ModelAndView fund() {
		User user = WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		if (WebHelper.checkSuperControl()) {
			mvc.setViewName(ViewPaths.MANAGE_FUND);
		} else {
			mvc.setViewName(ViewPaths.ERROR_VIEW);
		}
		return mvc;
	}

	/**
	 * 充值记录数据<br>
	 * super
	 * 
	 * @return
	 */
	@RequestMapping(value = "/fundData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse fundData(DataRequest dataRequest, long id) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		if (!user.getRole().toString().equals(Role.SUPER.toString())) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = fundService.getFundByUserId(id, pageSize, curpage);
		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;

	}

	@RequestMapping(value = "/view/viewFund", method = RequestMethod.GET)
	public ModelAndView viewfund(@RequestParam("id") long id) {
		User user = WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		double total = fundService.getTotal(id);
		mvc.addObject("total", total);
		User u = userBaseService.getUserById(id);
		mvc.addObject("user", u);
		if (WebHelper.checkSuperControl()) {
			mvc.setViewName(ViewPaths.MANAGE_VIEWFUND);
		} else {
			mvc.setViewName(ViewPaths.ERROR_VIEW);
		}
		return mvc;
	}

	/**
	 * 所有用户列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public ModelAndView user() {
		User user = WebHelper.getSessionUser();
		String role = user.getRole().toString();
		if (!role.equals(Role.ADMIN.toString())
				&& !role.equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}
		boolean tag = false;
		if (role.equals(Role.SUPER.toString())) {
			tag = true;
		}
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("tag", tag);
		mvc.setViewName(ViewPaths.MANAGE_USER);

		return mvc;
	}

	/**
	 * 所有普通用户列表数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/userData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse userData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = userBaseService.getAllBaseUsers(curpage, pageSize,
				Role.NORMAL);
		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 所有普通用户列表数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doctorData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse doctorData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = userBaseService.getAllBaseUsers(curpage, pageSize,
				Role.DOCTOR);
		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 所有用户列表数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allUserData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse allUserData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = userBaseService.getAllUsers(curpage, pageSize);
		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 所有用户列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/view/viewUser", method = RequestMethod.GET)
	public ModelAndView viewuser() {
		User user = WebHelper.getSessionUser();
		if (!user.getRole().toString().equals(Role.ADMIN.toString())
				&& !user.getRole().toString().equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.MANAGE_VIEWUSER);

		return mvc;
	}

	/**
	 * 执照列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/license", method = RequestMethod.GET)
	public ModelAndView license() {
		User user = WebHelper.getSessionUser();
		if (!user.getRole().toString().equals(Role.ADMIN.toString())
				&& !user.getRole().toString().equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.MANAGE_LICENSE);

		return mvc;
	}

	/**
	 * 执照列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/view/viewLicense", method = RequestMethod.GET)
	public ModelAndView viewlicense(@RequestParam("id") long id) {
		User user = WebHelper.getSessionUser();
		if (!user.getRole().toString().equals(Role.ADMIN.toString())
				&& !user.getRole().toString().equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}
		User u = userBaseService.getUserById(id);
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", u);
		mvc.setViewName(ViewPaths.MANAGE_VIEWLICENSE);

		return mvc;
	}

	/**
	 * 超级管理员得到管理员列表 任命 罢免 <br>
	 * 只会对超级管理员
	 * 
	 * @return
	 */
	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public ModelAndView admin() {
		User user = WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		if (WebHelper.checkSuperControl()) {
			mvc.setViewName(ViewPaths.MANAGE_ADMIN);
		} else {
			mvc.setViewName(ViewPaths.ERROR_VIEW);
		}
		return mvc;
	}

	/**
	 * 超级管理员得到管理员列表数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/adminData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse adminData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		if (!user.getRole().toString().equals(Role.SUPER.toString())) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = superService.getAdmins(curpage, pageSize);
		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 超级管理员得到管理员列表 任命 罢免 <br>
	 * 只会对超级管理员
	 * 
	 * @return
	 */
	@RequestMapping(value = "/view/viewAdmin", method = RequestMethod.GET)
	public ModelAndView viewadmin() {
		User user = WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		if (WebHelper.checkSuperControl()) {
			mvc.setViewName(ViewPaths.MANAGE_VIEWADMIN);
		} else {
			mvc.setViewName(ViewPaths.ERROR_VIEW);
		}
		return mvc;
	}

	/**
	 * 问卷管理
	 * 
	 * @return
	 */
	@RequestMapping(value = "/ppt", method = RequestMethod.GET)
	public ModelAndView ppt() {
		User user = WebHelper.getSessionUser();
		if (!user.getRole().toString().equals(Role.ADMIN.toString())
				&& !user.getRole().toString().equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.MANAGE_PPT);

		return mvc;
	}

	/**
	 * 问卷数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/pptData", method = RequestMethod.POST)
	public DataResponse pptData() {

		return null;
	}

	/**
	 * 问卷管理
	 * 
	 * @return
	 */
	@RequestMapping(value = "/view/viewPpt", method = RequestMethod.GET)
	public ModelAndView viewppt() {
		User user = WebHelper.getSessionUser();
		if (!user.getRole().toString().equals(Role.ADMIN.toString())
				&& !user.getRole().toString().equals(Role.SUPER.toString())) {
			throw new NotOperationEexception();
		}

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.MANAGE_VIEWPPT);

		return mvc;
	}

	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	public @ResponseBody
	void deleteUser(long userId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return;
		}

		if (user.getRole().toString().equals(Role.SUPER.toString())
				|| user.getRole().toString().equals(Role.ADMIN.toString())) {
			userBaseService.deleteUser(userId);
		}

	}
}
